#include <stdio.h>
#include <stdlib.h>

struct flex
{
    size_t count;
    double average;
    double scores[];
};

void showFlex(const struct flex * pt);

int main(void)
{
    struct flex * pt1, * pt2;
    int n = 5;
    int i;
    int tot = 0;
    
    //为结构和数组分配存储空间
    pt1 = malloc(sizeof(struct flex) + n * sizeof(double));
    pt1->count = n;
    for (i = 0; i < n; i++)
    {
        pt1->scores[i] = 20.0 - i;
        tot += pt1->scores[i];
    }
    pt1->average = tot / n;
    showFlex(pt1);

    n = 9;
    tot = 0;
    pt2 = malloc(sizeof(struct flex) + n * sizeof(double));
    pt2->count = n;
    for (i = 0; i < n; i++)
    {
        pt2->scores[i] = 20.0 - i / 2.0;
        tot += pt2->scores[i];
    }
    pt2->average = tot / n;
    showFlex(pt2);

    free(pt1);
    free(pt2);

    return 0;
}

void showFlex(const struct flex * pt)
{
    int i;
    printf("Scores: ");
    for (i = 0; i < pt->count; i++)
        printf("%g ", pt->scores[i]);
    printf("\nAverage: %g\n", pt->average);
}